package com.rokyinfo.pdm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rokyinfo.pdm.entity.SysUser;
import com.rokyinfo.pdm.service.IOauth2SecurityService;
import com.rokyinfo.pdm.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yuanzhijian on 2017/1/21.
 */
@Service
public class Oauth2SecurityServiceImpl implements IOauth2SecurityService {

    @Autowired
    private ISysUserService mISysUserService;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        SysUser obj = new SysUser();
        obj.setUsername(userName);
        SysUser mSysUser = mISysUserService.selectOne(new EntityWrapper<SysUser>(obj));
        if (mSysUser == null) {
            throw new UsernameNotFoundException(userName);
        }

        //定义权限集合
        List<SimpleGrantedAuthority> grantedAuthorities = new ArrayList<SimpleGrantedAuthority>();
        grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER"));
        boolean booleanStatus = true;
        if (mSysUser.getStatus() == 0) {
            booleanStatus = false;
        }
        User user = new User(mSysUser.getUsername(), mSysUser.getPassword(), booleanStatus, true, true, true, grantedAuthorities);
        return user;
    }

}
